mini.parse();

function GoodsAssociationHandler() {
};

GoodsAssociationHandler.instance = new GoodsAssociationHandler();

//获得全局的关联商品数据表格对象
var selectGrid = mini.get("selectGrid");

//获得全局的已选关联商品数据表格对象
var selectedGrid = mini.get("selectedGrid");

var isDouble="0";

// 加载所有关联商品
selectGrid.load();

// 加载已经关联的商品
selectedGrid.load({bgiId: bgiId});

//返回商品列表
GoodsAssociationHandler.returnGoods = function () {
	window.location.href = "goodsList.jsp";
};

/**
 * 刷新页面数据及布局
 */
GoodsAssociationHandler.afresh = function () {
	window.location.href="goodsAssociation.jsp?bgiId="+bgiId+"&bgiName="+bgiName;
};

/**
 * 初始化商品查询面板
 */
GoodsAssociationHandler.initQueryForm = function(){
	GoodsQueryHandler.getBgbCombobox();
	GoodsQueryHandler.getBgsCombobox();
	mini.get("queryGoodsCommend").setData(GoodsQueryHandler.goodsCommendTypeData);
	mini.get("queryGoodsCommend").setValue(GoodsQueryHandler.goodsCommendTypeData[0].id);
}
GoodsAssociationHandler.initQueryForm();

//查询可选商品列表
GoodsAssociationHandler.doQueryGoods = function () {
	var goodsQueryData = GoodsQueryHandler.goodsQueryForm.getData();
	var json = mini.encode(goodsQueryData);
   	selectGrid.load({goodsQueryData: json});
}

function onAssociationRenderer(e) {
	var mv = [ "单向", "双向" ];
	var z;
	
	try {
		z = parseInt(e.row.brgIsDouble);
	} catch (e) {
		return "";
	}
	
	if (z >= 0 && z <= 1)
		return mv[z];
	else
		return "";
}

function onActionAssociation(e) {
	var s = '<a class="New_Button" href="javascript:void(0);" onclick="removeGoodsAssoc();">删除</a>';
	return s;
}

function getSelectValue(val){
	isDouble = val;
}

function add() {        
    var items = selectGrid.getSelecteds();
	doAddItems(items);
}

function addAll() {        
    var items = selectGrid.getData();
    doAddItems(items);
}

function removeGoodsAssoc() {
    var items = selectedGrid.getSelecteds();
    selectedGrid.removeRows(items);
}       

function doAddItems(items) {
    items = mini.clone(items);
    
    //根据id判断，去除重复的item
    for (var i = items.length - 1; i >= 0; i--) {
        var item = items[i];
        item = {
        		_index: item._index, 
        		_uid: item._uid, 
        		bgiId: item.bgiId, 
        		bgiName: item.bgiName, 
        		brgIsDouble: isDouble
        };
        
        var isExist = selectedGrid.findRow(function (row) {
            if (row.bgiId == item.bgiId) return true;
        });
        
        if (!isExist) {
        	// 不能与自身建立关联
        	if(item.bgiId != bgiId){
        		selectedGrid.addRow(item);
        	}
        }
    }
}

function saveItems() {
	var data = selectedGrid.getData();
	data = mini.clone(data);
	var json = mini.encode(data);
	
	mini.confirm("确认提交更改？" ,"系统提示",
		function(action, value){
			if (action == "ok") {
				var ids = "";
				var isDoubles = "";
				
				for (var i = 0; i < data.length - 1; i++){
					ids += data[i].bgiId + ",";
					isDoubles += data[i].brgIsDouble + ",";
				}
				
				if(data.length != 0){
					ids += data[data.length - 1].bgiId;
					isDoubles += data[data.length - 1].brgIsDouble;
				}
				
                $.ajax({
//                    url: __root + "/addAssociation.do",
                	url: __root + "/saveBrgInfo.do",
                    data : {
		            	brgRgoods : ids,
		            	brgIsDoulbes : isDoubles,
		            	bgiId : bgiId,
		            	lslUser: gbOperateUser,
		    			lslAddress: gbOperateIp
		            },
                    success: function (text) {
                    	selectedGrid.unmask();
                    	MiniuiTools.miniInfo("系统提示","操作成功！", function(){
        				selectedGrid.reload();
        			});
                },
                error: function (jqXHR, textStatus, errorThrown) {
            			selectedGrid.unmask();
        				MiniuiTools.miniError("系统提示","操作失败！" + jqXHR.responseText);
                    }
                });	
			} 
		}
	)
}